﻿Imports System.Data.SqlClient
Public Class frm_rooms
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New frm_roomEdit()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then

            ReloatData()
        End If

    End Sub
    Private Sub ReloatData()
        Dim cn As New SqlConnection("SERVER = .; USER = sa; PASSWORD = 123; DATABASE = HOTELMANAGEMENT;")
        cn.Open()
        Dim cmd As New SqlCommand("SELECT tr.RoomID, rt.RoomType, rp.RoomPrice, tr.Floor, tr.RoomAdditional FROM TblRooms as tr, TblRoomType as rt, TblRoomPrice as rp WHERE tr.RoomTypeID = rt.RoomTypeID AND tr.RoomPriceID = rp.RoomPriceID", cn)

        Dim reader = cmd.ExecuteReader()
        Dim dt As New DataTable
        dt.Load(reader)
        reader.Close()
        Me.DataGridView1.DataSource = dt
    End Sub

    Private Sub showForm(ByVal form As Form)
        form.Show()
    End Sub
    Public Sub LoadData()
        Dim cn As New SqlConnection("SERVER = .; USER = sa; PASSWORD = 123; DATABASE = HOTELMANAGEMENT;")
        cn.Open()
        Dim cmd As New SqlCommand("SELECT tr.RoomID, rt.RoomType, rp.RoomPrice, tr.Floor, tr.RoomAdditional FROM TblRooms as tr, TblRoomType as rt, TblRoomPrice as rp WHERE tr.RoomTypeID = rt.RoomTypeID AND tr.RoomPriceID = rp.RoomPriceID", cn)

        Dim reader = cmd.ExecuteReader()
        Dim dt As New DataTable
        dt.Load(reader)
        reader.Close()
        Me.DataGridView1.DataSource = dt
    End Sub


    Private Sub frm_rooms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            LoadData()

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row As DataGridViewRow = Me.DataGridView1.SelectedRows(0)
        Dim frm As New frm_roomEdit()
        frm.txt_code.Text = row.Cells("txt_RoomID").Value
        frm.cob_r_type.Text = row.Cells("txt_RoomType").Value
        frm.cob_r_price.Text = row.Cells("txt_RoomPrice").Value
        frm.txt_floor.Text = row.Cells("txt_RoomFloor").Value
        frm.txt_des.Text = row.Cells("txt_Des").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If



    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            If Me.DataGridView1.SelectedRows.Count = 0 Then
                Exit Sub
            End If
            If MessageBox.Show("Are you sure want to delet?", "delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                Exit Sub

            End If
            Dim row As DataGridViewRow = Me.DataGridView1.SelectedRows(0)
            Dim id = row.Cells("txt_RoomID").Value

            Dim cn As New SqlConnection
            cn.ConnectionString = "SERVER = .; USER = sa; PASSWORD = 123; DATABASE = HOTELMANAGEMENT"
            cn.Open()

            Dim cmddel As New SqlCommand()
            cmddel.Connection = cn
            cmddel.CommandText = "DELETE FROM Tblrooms WHERE RoomID =" & id

            cmddel.ExecuteNonQuery()
            LoadData()


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
End Class